package com.etc.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Insets;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;

import com.etc.controller.CounterController;
import com.etc.controller.LoginController;
import com.etc.entity.Counter;
import com.etc.entity.Role;
import com.etc.service.RoleService;

public class GameLoading extends JFrame{
	
	JButton createRole = new JButton("创建人物");
	CounterController counterController = new CounterController();
	

	public GameLoading(Counter counter, DataOutputStream dos, DataInputStream dis, JPanel panel) {
		
		setLayout(new GridLayout(1, 10) );
		setFont(new Font("宋体",Font.BOLD,16));
		
		ArrayList<Role> roles = counter.getRoles();
		ArrayList<JPanel> jpanels = new ArrayList<>();
		ArrayList<JButton> jButtons = new ArrayList<>();
		
		for(int i=0; i<roles.size(); i++) {
			
			JPanel jPanel = new JPanel();
			jPanel.setBackground(Color.white);
			jPanel.setLayout(new GridLayout(10, 1, 5, 5));
			jPanel.add(new JLabel("        昵称:"+roles.get(i).getNickname()));
			jPanel.add(new JLabel("        职业名:"+roles.get(i).getCareer().getClassify().getClassName()+"        "));
			jPanel.add(new JLabel("        生命值:"+roles.get(i).getHeal()+"        "));
			jPanel.add(new JLabel("        魔法值:"+roles.get(i).getImage()+"         "));
			jPanel.add(new JLabel("        攻击值:"+roles.get(i).getAttack()+"         "));
			jPanel.add(new JLabel("        暴击值:"+roles.get(i).getCritical()+"         "));
			jPanel.add(new JLabel("        防御值:"+roles.get(i).getDefense()+"         "));
			jPanel.add(new JLabel("        速度值:"+roles.get(i).getSpeed()+"          "));
			
			JButton jButton = new JButton("选择人物");
			jPanel.add(jButton);
			jpanels.add(jPanel);
			jButtons.add(jButton);
			
			getContentPane().add(jpanels.get(i));
			
		}
		
		getContentPane().add(createRole);
		
		for(int i=0; i<jButtons.size(); i++) {
			Role role = roles.get(i);
			jButtons.get(i).addActionListener(e->{
				//获取此角色的索引
				int roleIndex = counterController.findRoleIndexInCounter(counter, role);
				String str = "addOnlineRole#" + counter.getId() + "#" + role.getRoleId();
				
				//将当前角色添加服务器中的在线角色
				String sendData = sendData(str, panel, dos, dis);
				
				FindCompetitor gl=new FindCompetitor(dos, dis, role, roleIndex, sendData, counter, panel);
				gl.setTitle("对战房间");
				gl.pack();
				gl.setVisible(true);
				gl.setBounds(0, 0, 450, 400);
				gl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				gl.setLocationRelativeTo(null);
							
//				JFrame frame=new JFrame("PK界面");
//				Mypanel mp = new Mypanel();
//				PrintPerson pp = new PrintPerson();		
//				pp.placeComponents(mp, role, counter, frame);
//				frame.add(mp);
//				frame.setSize(1200,800);
//				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//				frame.setVisible(true);
				setVisible(false);
			});
		}
		
		createRole.addActionListener(e->{
			
			JFrame frame=new JFrame("英雄选择界面");
			frame.setBounds(100, 100, 1500, 660);
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//			添加面板
			SelectHero sh=new SelectHero();
			sh.setText(sh, frame, counter, dos, dis, panel);
			frame.add(sh);
			frame.setVisible(true);
			setVisible(false);
		});
		
	}
	
	
	public String sendData(String str, JPanel panel,  DataOutputStream dos, DataInputStream dis) {
        try {
            dos.writeUTF(str);
            dos.flush();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(panel, "发送数据失败，可能连接已断开", "ERROR", JOptionPane.ERROR_MESSAGE);
            e.printStackTrace();
            System.exit(0);
        }

        String retStr = null;

        try {
            retStr = dis.readUTF();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(panel, "接收数据失败，可能连接已断开", "ERROR", JOptionPane.ERROR_MESSAGE);
            e.printStackTrace();
            System.exit(0);
        }
        return retStr;
	}
	
	//测试
	public static void main(String[] args) {
//		GameLoading gl=new GameLoading();
//		gl.setTitle("加载界面");
//		gl.pack();
//		gl.setVisible(true);
//		gl.setBounds(0, 0, 450, 400);
//		gl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//		gl.setLocationRelativeTo(null);
		
	}
	
}
